package com.diaoyanbao.model;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import com.diaoyanbao.util.FileFilter;
import com.diaoyanbao.util.FileSupport;

/**
 * common file adt
 * 
 * @author dingxs
 */
public abstract class FileVersionADT {
    protected TreeMap<String, String> fileVersionInfo;
    protected FileFilter fileFilter;
    protected List<String> filepathList;
    protected Logger logger = Logger.getLogger(FileVersionADT.class);

    public void initFileVersionInfo(String filePath) {
        fileVersionInfo = new TreeMap<String, String>();
        this.setFileFilter();
        getChildrenFilePath(filePath);
        try {
            for (String path : filepathList) {
                fileVersionInfo.put(FileSupport.truncateFileName(path, filePath).replace("\\", "/"), FileSupport.getFileMd5CheckSum(path));
            }
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            logger.error(e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.error(e.getMessage());
        }
    }

    public TreeMap<String, String> getFileVersionInfo() {
        return fileVersionInfo;
    }

    protected abstract void setFileFilter();

    private void getChildrenFilePath(String filePath) {
        File file = new File(filePath);
        filepathList = FileSupport.getChildrenFilepath(file, this.fileFilter, new ArrayList<String>());
    }
}
